# -*- coding:utf-8 -*-
from re import sub

def generate(name):
    """ 生成各种数据 """
    if name == 'template' and request.vars.has_key('templateid'):
        search_template = db(db.templates.id == request.vars['templateid']).select()[0]
        session.template_title = search_template['title']
        response.flash = "已选模板：" + session.template_title
        session.template_content = search_template['content']
    elif name == 'search_itmes' and request.vars.has_key('iid'):
        no_pic_path = 0
        session.iids = request.vars['iid']
        session.search_items = []
        for i in session.rsp_onsale['items']:
            if i['iid'] in session.iids:
                try:
                    session.search_items.append({'iid':i['iid'], 'pic_path':i['pic_path']})
                except Exception:
                    no_pic_path += 1
        
        if no_pic_path > 0:
            response.flash = "有 %d 件商品，没有可以显示的图片" % no_pic_path
    else:
        return name

def show(name):
    """ 显示各种内容 """

    if name == 'template':
        if session.template_content and session.search_items:
            # 填充内容到模板里
            stock_images = ''
            for i in session.search_items:
                image_path = IMG(_src=i['pic_path']+'_sum.jpg')
                stock_images += "<a href='%s' target='_blank'>%s</a> " % (sip.get_item_url(i['iid']), str(image_path))
            session.template_content = sub('<span>images</span>', str(stock_images), session.template_content)
